{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 通用函数",
   "id": "1c475f7fd8960494"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 1 一元通用函数\n",
    "- 接受一个数组参数，返回一个转换后的新数组。，比如 sqrt 和 exp 函数：就是一元通用函数"
   ],
   "id": "12969f43dcd72770"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-29T02:29:52.298515Z",
     "start_time": "2025-08-29T02:29:52.291952Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "arr1 = np.array([4,9,16,25])\n",
    "print(arr1)\n",
    "print(np.sqrt(arr1)) # 返回数组元素的正平方根\n",
    "print(np.exp(arr1)) # 计算每个元素的自然指数值 e(约等于 2.71828)的x次方"
   ],
   "id": "94b618d2f580d5e7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 4  9 16 25]\n",
      "[2. 3. 4. 5.]\n",
      "[5.45981500e+01 8.10308393e+03 8.88611052e+06 7.20048993e+10]\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 2. 二元通用函数\n",
    "- 接受两个数组参数，返回一个操作后的新数组。，比如 add 和 maximum 则会接受两个数组并返回一个数组结果，所以叫做二元通用函数。"
   ],
   "id": "459a6dfc70856fc0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-29T02:36:43.532753Z",
     "start_time": "2025-08-29T02:36:43.522533Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr2 = np.array([4,9,16,25])\n",
    "arr3 = np.array([1,2,3,32])\n",
    "print(np.add(arr2,arr3))\n",
    "print(np.maximum(arr2,arr3)) # 对位比较大小，取大的，生成新的数组返回，逐个元素地将 x和 y 中元素的最大值计算出来"
   ],
   "id": "64c314bbafce4c20",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 5 11 19 57]\n",
      "[ 4  9 16 32]\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 3. 函数",
   "id": "41392b29db6ff07c"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 3.1 数学运算函数",
   "id": "9898a209ca3af211"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-29T02:55:35.099803Z",
     "start_time": "2025-08-29T02:55:35.091056Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr4 = np.array([4,9,16,25,])\n",
    "\n",
    "print(f'绝对值:{np.abs(arr4)}')\n",
    "print(f'平方根:{np.sqrt(arr4)}')\n",
    "print(f'平方:{np.square(arr4)}')\n",
    "print(f'3次幂：{np.power(arr4,3)}')"
   ],
   "id": "d4a49c2f72f5772c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "绝对值:[ 4  9 16 25]\n",
      "平方根:[2. 3. 4. 5.]\n",
      "平方:[ 16  81 256 625]\n",
      "3次幂：[   64   729  4096 15625]\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 3.2 对数函数",
   "id": "89065392c5b72e18"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-29T03:04:26.306956Z",
     "start_time": "2025-08-29T03:04:26.297393Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr5 = np.array([64,729,4096,15625,100])\n",
    "\n",
    "print(f'自然对数：{np.log(arr5)}') # 自然对数就是以数学常数 e（约等于 2.71828...）为底数的对数。\n",
    "print(f'以10为底的对数:{np.log10(arr5)}')\n",
    "print(f'以2为底数的对数:{np.log2(arr5)}')\n",
    "print(f'以e为底，数组元素+1的对数:{np.log1p(arr5)}') # 还是以e为底 数组元素 +1 的对数，元素越趋近于0，值越精确\n",
    "print(f'以e为底，数组元素+1的对数：{np.log(arr5+1)}')"
   ],
   "id": "8b6372da6a229d9c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "自然对数：[4.15888308 6.59167373 8.31776617 9.65662747 4.60517019]\n",
      "以10为底的对数:[1.80617997 2.86272753 3.61235995 4.19382003 2.        ]\n",
      "以2为底数的对数:[ 6.          9.509775   12.         13.93156857  6.64385619]\n",
      "dd:[4.17438727 6.59304453 8.31801028 9.65669147 4.61512052]\n",
      "自然对数：[4.17438727 6.59304453 8.31801028 9.65669147 4.61512052]\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 数据清洗\n",
    "- np.isfinite(arr) # 判断一个数值是否是有限的正常数字"
   ],
   "id": "cbc10a1477726ab"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-29T03:49:54.218913Z",
     "start_time": "2025-08-29T03:49:54.209417Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 处理包含 NaN 和 inf 的数据\n",
    "data = np.array([1, 2, np.nan, np.inf, 5])\n",
    "print(data)\n",
    "\n",
    "clean_data = data[np.isfinite(data)]  # 筛选有效数据\n",
    "print(clean_data)  # [1, 2, 5]"
   ],
   "id": "8c1e782a9eb96895",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.  2. nan inf  5.]\n",
      "[1. 2. 5.]\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-29T03:51:27.322722Z",
     "start_time": "2025-08-29T03:51:27.309958Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 对数值特征进行多种变换\n",
    "features = np.random.randn(100, 3)\n",
    "print(features)\n",
    "\n",
    "print('=' * 30)\n",
    "log_features = np.log1p(features)  # 对数变换\n",
    "sqrt_features = np.sqrt(np.abs(features)) * np.sign(features)  # 带符号的平方根\n"
   ],
   "id": "939b2a32854119a4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1.39811513 -0.24390707 -1.16280724]\n",
      " [-0.30513921 -0.78638455  1.16955104]\n",
      " [ 0.7039117  -1.73357082 -0.39119134]\n",
      " [-0.13393241 -1.27489637  1.1306468 ]\n",
      " [-0.24915705 -0.35306619  0.49272591]\n",
      " [ 0.36200758  1.33218318 -1.01740167]\n",
      " [ 0.41803934 -0.14978639  1.07255745]\n",
      " [ 0.88379954 -0.54277258 -0.45825574]\n",
      " [ 0.74175878  0.84451786 -0.12924118]\n",
      " [-0.1378538   1.16103613  1.81293141]\n",
      " [ 1.81322951 -0.17317228 -0.29932589]\n",
      " [-1.5049242   1.30424611  1.78774133]\n",
      " [-2.02819782  1.34337772  0.67212947]\n",
      " [ 0.35999059 -0.58904376 -0.88617688]\n",
      " [ 0.43567506  0.86756304 -0.05313061]\n",
      " [ 0.47054974 -1.1486215   0.53557929]\n",
      " [-0.22426864 -0.17885806 -0.10883529]\n",
      " [-0.61390501  1.38765383 -0.66760415]\n",
      " [ 0.91900884  0.94153336  3.29153673]\n",
      " [ 0.57981635 -2.09014721  1.78596824]\n",
      " [ 0.85185072  1.85354433  1.92141194]\n",
      " [ 0.12863913 -2.61543601  0.48789771]\n",
      " [ 2.62048599  0.6973734   1.50115057]\n",
      " [-0.52557013 -0.71885693 -0.49327299]\n",
      " [-0.46377393  1.63405105  0.68743654]\n",
      " [-0.21675179 -0.2408325   0.00927634]\n",
      " [-0.19040951 -0.33768751  0.24283528]\n",
      " [ 0.71262722  0.82427217 -0.76385758]\n",
      " [-1.67873506 -0.32272682  0.20360017]\n",
      " [ 0.06223368  0.82354742 -0.28983356]\n",
      " [-2.22117859 -0.4199313   0.37115817]\n",
      " [-1.57923423  0.97420665 -0.19738979]\n",
      " [-1.98807204 -1.46116964  1.88777581]\n",
      " [ 0.97376089  0.21190462 -0.5900665 ]\n",
      " [-0.57594534 -0.91445427 -0.76105448]\n",
      " [-1.4534037  -1.10520893  0.79923776]\n",
      " [ 0.5660919  -0.0098384   1.23203981]\n",
      " [-0.09317835 -1.1350235   0.94103735]\n",
      " [ 1.05779866 -0.20311949  0.52973243]\n",
      " [ 0.23361036  0.29887172 -1.67601712]\n",
      " [-0.68153811  1.11796487  1.55430932]\n",
      " [-0.0430265   1.99839593 -0.85857415]\n",
      " [-1.35745947 -0.59362482  0.92875959]\n",
      " [-0.91617857  0.10625255 -0.61266469]\n",
      " [-0.61013023 -2.54179945  0.01598137]\n",
      " [-0.30995125  1.17983852 -0.02463675]\n",
      " [-0.83946384 -0.47206175  1.50114668]\n",
      " [-1.03551654 -0.60279254  0.09040016]\n",
      " [ 0.68951518 -0.73355044 -0.68247007]\n",
      " [ 0.70325026  1.37454884 -0.23088021]\n",
      " [-0.31152262 -1.55293579 -0.87981742]\n",
      " [-0.39027427  0.26998942  0.48502383]\n",
      " [ 0.11677032 -0.30581687  0.04698248]\n",
      " [-1.0196964   0.16345642 -1.72965889]\n",
      " [-0.93263443 -0.82345874 -0.13228552]\n",
      " [-0.42793404  0.21642981 -0.00965477]\n",
      " [ 1.65333156 -1.21904591 -0.60347091]\n",
      " [-0.14179928 -1.0831973   3.07653311]\n",
      " [-0.48081127  0.21342085 -0.17848286]\n",
      " [ 0.97779313 -0.45244229  1.20945589]\n",
      " [-0.74008439 -2.1480543  -0.03362307]\n",
      " [-2.41217419 -0.69878942  0.28406263]\n",
      " [-0.88234333 -0.00784251  0.25819542]\n",
      " [-0.64748907  0.60858161 -1.50439041]\n",
      " [ 0.6234785  -0.66919839  0.19700234]\n",
      " [-1.15867603  2.20255726 -1.30383157]\n",
      " [ 1.01730996  0.23383907  0.11541168]\n",
      " [ 0.54757643  2.08587085  0.80116427]\n",
      " [ 0.30606269  1.76496029 -1.4283278 ]\n",
      " [-0.13612521  0.71263087  0.7351375 ]\n",
      " [ 1.18362633 -0.74015792  1.66879841]\n",
      " [ 1.04080544 -0.89801165  0.35063895]\n",
      " [ 0.85057556  0.48490211  0.67583075]\n",
      " [-2.12138231  1.1999163  -1.16339392]\n",
      " [-0.44475478 -0.43064941  0.39060128]\n",
      " [ 1.18070084 -1.07628598 -0.66878254]\n",
      " [-0.64523671  1.28019286  0.48896476]\n",
      " [ 0.02153804 -1.97159707 -0.30451637]\n",
      " [-0.4590679   0.15672315 -1.17649828]\n",
      " [ 0.9443289   0.30768401 -2.03732526]\n",
      " [-0.16177676 -1.75600304 -0.08871384]\n",
      " [-1.27475988  0.18410939  0.07604527]\n",
      " [-0.10991099 -0.36836992 -1.25241218]\n",
      " [-0.7848914  -0.47505311 -0.85364212]\n",
      " [ 1.08798318  0.13291933 -0.59836576]\n",
      " [-0.53399079  0.42786893 -1.39056253]\n",
      " [-0.78983426 -0.57011557 -0.4816064 ]\n",
      " [-0.01348118 -2.12736088  0.20792561]\n",
      " [-0.09417493 -1.20483661  0.76910693]\n",
      " [ 0.41158251  0.30240553 -0.2013042 ]\n",
      " [-1.38193253 -1.72776781  2.63958941]\n",
      " [-0.38776279 -2.19487672 -0.86611059]\n",
      " [-2.04574006  0.35238578 -0.98451109]\n",
      " [-2.77813619  0.07602335 -0.60622483]\n",
      " [-1.55764654 -1.00586807 -0.12989462]\n",
      " [ 1.04382745 -0.64706685  0.7868852 ]\n",
      " [-0.38257845  0.89239708  1.49471754]\n",
      " [ 0.04115076  0.07758501 -0.53304055]\n",
      " [ 0.55556483 -0.45526689  1.42429576]\n",
      " [-0.39515312  0.91447947  0.76019729]]\n",
      "==============================\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\16357\\AppData\\Local\\Temp\\ipykernel_18020\\1409429100.py:6: RuntimeWarning: invalid value encountered in log1p\n",
      "  log_features = np.log1p(features)  # 对数变换\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-29T04:00:28.634661Z",
     "start_time": "2025-08-29T04:00:28.628948Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 多条件筛选\n",
    "scores = np.array([85, 92, 78, 60, 95, 53])\n",
    "good_scores = scores[(scores >= 80) & (scores <= 90)]  # 80-90 分的成绩\n",
    "print(good_scores)  # [85]"
   ],
   "id": "ff3eeadaaa27ad28",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[85]\n"
     ]
    }
   ],
   "execution_count": 36
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
